package com.chenliwu.demo.annotation.example1;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 自定义注解示例
 *
 * @author chenliwu
 * @since 2022-04-05
 */
@MyCustomAnnotation(value = "我是指定注解value")
public class TestAnnotation {

    @MyCustomAnnotation(value = "类字段注解1")
    public String field1;

    @MyCustomAnnotation(value = "类字段注解2")
    protected String field2;

    @MyCustomAnnotation(value = "类字段注解3")
    private String field3;

    @MyCustomAnnotation(value = "方法注解1")
    public void fun1() {

    }

    @MyCustomAnnotation(value = "方法注解2")
    protected void fun2() {

    }


    @MyCustomAnnotation(value = "方法注解3")
    private void fun3() {

    }

    public static void main(String[] args) {
        // fetchClassInfo();

        // fetchMethodInfo();

        fetchFieldInfo();
    }

    static void fetchFieldInfo() {
        // getFields()：获取所有Public字段
        // Field[] fields = TestAnnotation.class.getFields();

        // getDeclaredFields()：获取所有字段，包括public、protected、private
        Field[] fields = TestAnnotation.class.getDeclaredFields();

        System.out.println("\nfetchFieldInfo\n");
        for (Field field : fields) {
            if (field.isAnnotationPresent(MyCustomAnnotation.class)) {
                MyCustomAnnotation myCustomAnnotation = field.getAnnotation(MyCustomAnnotation.class);
                System.out.println("myController.value :" + myCustomAnnotation.value() + ", myController.des:" + myCustomAnnotation.des());
                System.out.println();
            }
        }
    }

    static void fetchMethodInfo() {
        // getMethods()：反射获取所有public方法
        // Method[] methods = TestAnnotation.class.getMethods();

        // getDeclaredMethods(): 获取所有方法，包括public、protected、private
        Method[] methods = TestAnnotation.class.getDeclaredMethods();

        System.out.println("\nfetchMethodInfo\n");
        for (Method method : methods) {
            // 判断方法是否有MyAnnotation注解
            if (method.isAnnotationPresent(MyCustomAnnotation.class)) {
                MyCustomAnnotation myCustomAnnotation = method.getAnnotation(MyCustomAnnotation.class);
                System.out.println("myController.value :" + myCustomAnnotation.value() + ", myController.des:" + myCustomAnnotation.des());
                System.out.println();
            }

        }
    }

    static void fetchClassInfo() {
        try {
            TestAnnotation testAnnotation = new TestAnnotation();
            Class<? extends TestAnnotation> aClass = testAnnotation.getClass();
            //判断person对象上是否有Info注解
            if (aClass.isAnnotationPresent(MyCustomAnnotation.class)) {
                System.out.println("TestAnnotation类上配置了MyController注解！");
                //获取该对象上Info类型的注解
                MyCustomAnnotation myCustomAnnotation = aClass.getAnnotation(MyCustomAnnotation.class);
                System.out.println("myController.value :" + myCustomAnnotation.value() + ", myController.des:" + myCustomAnnotation.des());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
